package com.homer.commons.sscpprotocol.server.impl;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.homer.commons.sscpprotocol.server.servlet.ServletContext;

public class ServletContextImpl
  implements ServletContext
{
  private String rootPath = System.getProperty("user.dir");
  private Map<String, Object> attributes;

  public String getRootPath()
  {
    return this.rootPath;
  }

  public void setRootPath(String rootPath) {
    this.rootPath = rootPath;
  }

  public String getRealPath(String path) {
    return getResource(path).getAbsolutePath();
  }

  public File getResource(String path) {
    return new File(this.rootPath, path);
  }

  public InputStream getResourceAsStream(String path)
  {
    try
    {
      File file = getResource(path);
      return !file.isFile() ? null : new FileInputStream(file); } catch (Exception e) {
    }
    return null;
  }

  public void clearAttributes()
  {
    if (this.attributes != null)
      this.attributes.clear();
  }

  public Object getAttribute(String name)
  {
    return this.attributes == null ? null : this.attributes.get(name);
  }

  public Object removeAttribute(String name) {
    return this.attributes == null ? null : this.attributes.remove(name);
  }

  public synchronized void setAttribute(String name, Object value) {
    if (this.attributes == null) {
      this.attributes = new HashMap();
    }
    this.attributes.put(name, value);
  }
}
